﻿/*
创建人：zhangrj
日期：2023-06-15
描述：热表合卡批次管理页面js
*/
var page = {
    rootPath: getRootPath(), //网站根路径
    url: getRootPath() + "/Ashx/ContainerCombinateHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        unDisPage.bind(); //未派工页面
        disPage.bind(); //已派工页面
        cbPage.bind(); //已合卡页面
    },
    //页面事件绑定
    bind: function () {
        $("#divIsInstore").hide(); //默认隐藏是否入库
        //查询按钮
        $('#btnSearch').on("click", function () {
            page.resetPage(false, true, true);
            var tab = $('#tt').tabs('getSelected');
            var index = $('#tt').tabs('getTabIndex', tab);
            switch (index) {
                case 0: //为派工查询
                    unDisPage.pageNumber = 1;
                    unDisPage.search();
                    break;
                case 1: //已派工查询
                    disPage.pageNumber = 1;
                    disPage.search();
                    break;
                case 2: //已合卡查询
                    cbPage.pageNumber = 1;
                    cbPage.search();
                    break;
                default:
                    displayMessage("未知功能", false);
                    break;
            }
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.resetPage(true, true, true);
        });
        $('#tt').tabs({
            border: false,
            onSelect: function (title, index) {
                switch (index) {
                    case 0:
                        $("#divIsInstore").hide();
                        break;
                    case 1:
                        $("#divIsInstore").hide();
                        break;
                    case 2:
                        $("#divIsInstore").show();
                        break;
                    default: break;
                }
            }
        });
    },
    //重置页面（查询，结果列表,分页）
    resetPage: function (query, list, pager) {
        if (query) {
            $("#txtMfgOrderName").textbox("setValue", ""); //订单号
            $("#txtProductName").textbox("setValue", ""); //件号
            $('#selIsInstore').combobox("setValue", "0"); //是否入库
        }
        if (list) {
            clearEasyuiDataGrid("gdUnDisContainerInfo");
            clearEasyuiDataGrid("gdDisContainerInfo");
            clearEasyuiDataGrid("gdCombinateInfo");
            clearEasyuiDataGrid("gdContainerDetail");
        }
        if (pager) {
            //未派工页面分页参数重置
            unDisPage.pageNumber = 1; unDisPage.pageTotal = 0
            $('#ppUnDis').pagination('refresh', {
                total: unDisPage.pageTotal,
                pageSize: unDisPage.pageSize,
                pageNumber: unDisPage.pageNumber
            });
            //已派工页面分页参数重置
            disPage.pageNumber = 1; disPage.pageTotal = 0
            $('#ppDis').pagination('refresh', {
                total: disPage.pageTotal,
                pageSize: disPage.pageSize,
                pageNumber: disPage.pageNumber
            });
            //已合卡页面分页参数重置
            cbPage.pageNumber = 1; cbPage.pageTotal = 0
            $('#ppCb').pagination('refresh', {
                total: cbPage.pageTotal,
                pageSize: cbPage.pageSize,
                pageNumber: cbPage.pageNumber
            });
        }
    },
    //禁用页面按钮
    disableBtn: function () {
        $("body").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('disable');
        });
    },
    //启用页面按钮
    enableBtn: function () {
        $("body").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('enable');
        });
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//未派工
var unDisPage = {
    pageTotal: 0,
    pageSize: 20,
    pageNumber: 1,
    bind: function () {
        customizedEasyuiPagination('ppUnDis'); //定制化分页
        //列表加载
        $("#gdUnDisContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#ppUnDis').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: unDisPage.pageTotal,
                    pageSize: unDisPage.pageSize,
                    pageNumber: unDisPage.pageNumber
                });
                customizedEasyuiPagination('ppUnDis'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'FACONTAINERNAME', title: '主制批次', width: 150 },
                 { field: 'CONTAINERNAME', title: '批次', width: 200 },
                 { field: 'PRODUCTNAMEANDREV', title: '件号', width: 180 },
                 { field: 'WORKFLOWNAMEANDREV', title: '工艺', width: 180 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 150 },
                 { field: 'QTY', title: '在制数', width: 60 },
                 { field: 'CREATEDATE', title: '开卡日期', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 }
                ]]
        });
        //分页事件
        $('#ppUnDis').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('ppUnDis'); //定制化分页
                unDisPage.pageSize = pageSize; unDisPage.pageNumber = pageNumber;
                unDisPage.search();
            }
        });
        //绑定分页go按钮
        $('#unDisPageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            unDisPage.pageNumber = pageNumber;
            unDisPage.search();
        });
        //合卡
        $('#btnUnDisCombinate').on("click", function () {
            unDisPage.save();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetRBUnDispatchContainer",
            pageSize: unDisPage.pageSize,
            pageIndex: unDisPage.pageNumber,
            orderName: $("#txtMfgOrderName").textbox("getValue"),
            productName: $("#txtProductName").textbox("getValue")
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            unDisPage.pageTotal = res.RowCount;
            $("#gdUnDisContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //保存
    save: function () {
        page.disableBtn();
        var selected = $("#gdUnDisContainerInfo").datagrid("getChecked");
        if (selected.length === 0) {
            page.enableBtn();
            displayMessage("未选择数据", false);
            return false;
        }
        var formData = {
            action: "SaveContainerCombinateData",
            container: JSON.stringify(selected),
            optype: 0
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                unDisPage.search();
            }
            page.enableBtn();
            displayMessage(res.Message, res.Result);
        });
    }
};
//已派工（但未报工）
var disPage = {
    pageTotal: 0,
    pageSize: 20,
    pageNumber: 1,
    bind: function () {
        customizedEasyuiPagination('ppDis'); //定制化分页
        //列表加载
        $("#gdDisContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#ppDis').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: disPage.pageTotal,
                    pageSize: disPage.pageSize,
                    pageNumber: disPage.pageNumber
                });
                customizedEasyuiPagination('ppDis'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'FACONTAINERNAME', title: '主制批次', width: 150 },
                 { field: 'CONTAINERNAME', title: '批次', width: 200 },
                 { field: 'PRODUCTNAMEANDREV', title: '件号', width: 180 },
                 { field: 'WORKFLOWNAMEANDREV', title: '工艺', width: 180 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 150 },
                 { field: 'QTY', title: '在制数', width: 60 },
                 { field: 'CREATEDATE', title: '开卡日期', width: 80,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 }
                ]]
        });
        //分页事件
        $('#ppDis').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('ppDis'); //定制化分页
                disPage.pageSize = pageSize; disPage.pageNumber = pageNumber;
                disPage.search();
            }
        });
        //绑定分页go按钮
        $('#disPageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            disPage.pageNumber = pageNumber;
            disPage.search();
        });
        $('#btnDisCombinate').on("click", function () {
            disPage.save();
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetRBDispatchContainer",
            pageSize: disPage.pageSize,
            pageIndex: disPage.pageNumber,
            orderName: $("#txtMfgOrderName").textbox("getValue"),
            productName: $("#txtProductName").textbox("getValue")
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            disPage.pageTotal = res.RowCount;
            $("#gdDisContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //保存
    save: function () {
        page.disableBtn();
        var selected = $("#gdDisContainerInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("未选择数据", false);
            return false;
        }
        var formData = {
            action: "SaveContainerCombinateData",
            container: JSON.stringify(selected),
            optype: 1
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                disPage.search();
            }
            page.enableBtn();
            displayMessage(res.Message, res.Result);
        });
    }
};
//已合卡
var cbPage = {
    pageTotal: 0,
    pageSize: 20,
    pageNumber: 1,
    bind: function () {
        customizedEasyuiPagination('ppCb'); //定制化分页
        //列表加载
        $("#gdCombinateInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#ppCb').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: cbPage.pageTotal,
                    pageSize: cbPage.pageSize,
                    pageNumber: cbPage.pageNumber
                });
                customizedEasyuiPagination('ppCb'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'COMBINATENAME', title: '合卡名', width: 120 },
                 { field: 'PRODUCTNAMEANDREV', title: '件号', width: 180 },
                 { field: 'WORKFLOWNAMEANDREV', title: '工艺', width: 180 },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 150 },
                 { field: 'QTY', title: '在制数', width: 60 }
                ]],
            onClickRow: function (rowIndex, rowData) {
                $("#gdContainerInfo").datagrid("uncheckAll");
                $("#gdContainerInfo").datagrid("checkRow", rowIndex);
                cbPage.showDetail(rowData);
            }
        });
        //批次明细列表加载
        $("#gdContainerDetail").datagrid({
            columns: [[
                 { field: 'CONTAINERNAME', title: '批次', width: 200 },
                 { field: 'QTY', title: '在制数', width: 60 }
                ]]
        });
        //分页事件
        $('#ppCb').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('ppCb'); //定制化分页
                cbPage.pageSize = pageSize; cbPage.pageNumber = pageNumber;
                cbPage.search();
            }
        });
        //绑定分页go按钮
        $('#cbPageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            cbPage.pageNumber = pageNumber;
            cbPage.search();
        });
        //删除合卡
        $('#btnDele').on("click", function () {
            cbPage.dele();
        });
    },
    //详细批次
    showDetail: function (row) {
        clearEasyuiDataGrid("gdContainerDetail");
        //加载数据
        var formData = {
            action: "GetRBContainerCombinateDetail",
            combinateName: row.COMBINATENAME
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#gdContainerDetail").datagrid('loadData', { rows: res.DBTable });
        });
    },
    //查询
    search: function () {
        var formData = {
            action: "GetRBContainerCombinateDataList",
            pageSize: cbPage.pageSize,
            pageIndex: cbPage.pageNumber,
            orderName: $("#txtMfgOrderName").textbox("getValue"),
            productName: $("#txtProductName").textbox("getValue"),
            isInstore: $("#selIsInstore").combobox("getValue")
        };
        page.post(page.url, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            cbPage.pageTotal = res.RowCount;
            $("#gdCombinateInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //删除合卡
    dele: function () {
        var selected = $("#gdCombinateInfo").datagrid("getChecked");
        if (selected.length === 0) {
            displayMessage("未选择数据", false);
            return false;
        }
        var combinatename = selected.map(function (item) {
            return {
                combinatename: item.COMBINATENAME
            }
        });
        var formData = {
            action: "DeleContainerCombinateData",
            combinatename: JSON.stringify(combinatename)
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                cbPage.search();
            }
            displayMessage(res.Message, res.Result);
        });
    }
};
$(function () {
    page.init();
});